package
{
	import org.flixel.*;


	public class RateState extends FlxState
	{
		protected var rBox:RatingBox;
		protected var ratingBounds:FlxSprite;
		protected var puzSpec:PuzzleSpecification;
		protected var win:Boolean;
		protected var hasRated:Boolean;
		protected var challengePuzzle:Boolean;
		protected var isQuickPlay:Boolean;
		protected var fromFriend:Boolean;
		protected var friendName:String;
		protected var friendID:String;
		
		//puzzleChallenging
		protected var challenge:FlxSprite;
		protected var modalPopup:Boolean;
		protected var challengePopup:ChallengeFriendsComponent;
		
		protected var delayTimer:Number;
		protected var aboutToSwitch:Boolean;
		
		public function RateState(puzSpec:PuzzleSpecification, challengePuzzle:Boolean, fromFriend:Boolean, isQuickPlay:Boolean, win:Boolean, hasRated:Boolean, friendName:String, friendID:String)
		{
			this.challengePuzzle = challengePuzzle;
			delayTimer = -1;
			this.fromFriend = fromFriend;
			this.friendName = friendName;
			this.friendID = friendID;
			this.hasRated = hasRated;
			this.puzSpec = puzSpec;
			this.win = win;
			modalPopup = false;
			this.isQuickPlay = isQuickPlay;
			
			if (win) {
				if (Elemental.MODE == Elemental.FACEBOOK_MODE) {
					Client.recordPuzzleWin(Elemental.fbInfo.userID, puzSpec.getMapID());
				}
				add(new FlxSprite(0,0,Res.LevelCompleteImage));
			} else {
				add(new FlxSprite(0,0,Res.LoseWolfScreen));
			}
			
			rBox = new RatingBox((FlxG.width - 300)/2, 400);
			ratingBounds = new FlxSprite((FlxG.width - 300)/2, 400, Res.blankImage);
			ratingBounds.width = 300;
			ratingBounds.height = 80;
			if (!hasRated) {
				this.add(rBox);
			}
			
			if (Elemental.MODE == Elemental.FACEBOOK_MODE) {
				createPuzzleChallenges()
			}
		}
		
		public override function update():void {
			super.update();
			
			
			if (aboutToSwitch) {
				delayTimer -= FlxG.elapsed;
				if (delayTimer < 0) {
					if (!win) {
						FlxG.switchState(new PuzzlePlayingState(puzSpec, challengePuzzle, fromFriend, isQuickPlay, false, rBox.getRatingValue() || hasRated, friendName, friendID));
					} else {
						if (isQuickPlay) {
							FlxG.switchState(new QuickPlayState());
						} else if (fromFriend) {
							FlxG.switchState(new FriendRoomState(friendID, friendName));
						} else {
							if (challengePuzzle) {
								FlxG.switchState(new PlayerRoomState());
							} else {
								FlxG.switchState(new LoadPuzzlesState());
							}
						}
					}
				}
			}
			
			if (FlxG.mouse.justPressed()) {
				if (modalPopup) {
					if (challengePopup.finishedChallenging()) {
						this.remove(challengePopup, true);
						modalPopup = false;
					}
				} else {
					if (challenge != null && challenge.overlapsPoint(FlxG.mouse.x, FlxG.mouse.y)) {
						checkForChallenge();
					} else if (!ratingBounds.overlapsPoint(FlxG.mouse.x, FlxG.mouse.y)) {
						if (rBox.hadRated()) {
							Client.submitRating(puzSpec.getMapID(), rBox.getRatingValue());
							delayTimer = 1.5;
						}
						aboutToSwitch = true;
					}
				}
			}
		}
		
		private function checkForChallenge():void {
			modalPopup = true;
			var xpos:int = (FlxG.width - ChallengeFriendsComponent.WIDTH)/2;
			var ypos:int = (FlxG.height - ChallengeFriendsComponent.HEIGHT)/2;
			challengePopup = new ChallengeFriendsComponent(puzSpec.getMapID(), xpos, ypos);
			add(challengePopup);
		}
		
		private function createPuzzleChallenges():void {
			challenge = new FlxSprite((FlxG.width - 300)/2 + 150, 490, Res.challengeFriends);
			add(challenge);
		}
	}
}